package com.ujiuye.Servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/firstServlet")
public class FirstServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext
//        ServletContext sc=getServletContext();
//        //获取ServletContext中的数据
//        System.out.println(sc.getInitParameter("sname"));
//        //获取ServletContext对象中所有数据的name
//        Enumeration<String> names=sc.getInitParameterNames();
//        while (names.hasMoreElements()){
//            String name=names.nextElement();
//            String value=sc.getInitParameter(name);
//            System.out.println(name+":"+value);
//        }
        //创建cookie
        Cookie cookie=new Cookie("sname","mary");
        //设置时效
        cookie.setMaxAge(60*60*24*365);
        //将cookie放到响应当中，让响应对象将其带回客户端
        resp.addCookie(cookie);
        resp.sendRedirect("result.jsp");
    }
}
